<?php
// +----------------------------------------------------------------------
// | RXThinkCMF敏捷开发框架 [ 赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2023 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | Licensed LGPL-3.0 RXThinkCMF并不是自由软件，未经许可禁止去掉相关版权
// +----------------------------------------------------------------------
// | 官方网站: https://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: @牧羊人 团队荣誉出品
// +----------------------------------------------------------------------
// | 版权和免责声明:
// | 本团队对该软件框架产品拥有知识产权（包括但不限于商标权、专利权、著作权、商业秘密等）
// | 均受到相关法律法规的保护，任何个人、组织和单位不得在未经本团队书面授权的情况下对所授权
// | 软件框架产品本身申请相关的知识产权，禁止用于任何违法、侵害他人合法权益等恶意的行为，禁
// | 止用于任何违反我国法律法规的一切项目研发，任何个人、组织和单位用于项目研发而产生的任何
// | 意外、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、
// | 附带或衍生的损失等)，本团队不承担任何法律责任，本软件框架禁止任何单位和个人、组织用于
// | 任何违法、侵害他人合法利益等恶意的行为，如有发现违规、违法的犯罪行为，本团队将无条件配
// | 合公安机关调查取证同时保留一切以法律手段起诉的权利，本软件框架只能用于公司和个人内部的
// | 法律所允许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\model\Annual;
use app\admin\model\Data as DataModel;
use app\admin\model\DataClass;
use app\admin\model\DataClasses;
use app\admin\model\DataProf;
use app\admin\model\SportRecord;
use think\Db;


class Data extends Backend
{
    /**
     * 初始化方法
     * @author 牧羊人
     * @since: 2023/09/02
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
       // $this->service = new ClssdService();
    }

    //学校画像
    //数据中心
    public function school()
    {
        $map = whereSchoolMap(false);
        $canyu_num = DataModel::ticeren($map);
        $canyu_rate = cacl_rate($canyu_num,\app\admin\model\Memb::allNum());
        $hege = DataModel::ticerenHege($map);
        $hege_rate = 0;
        if($hege > 0){
            $hege_rate = cacl_rate($hege,$canyu_num).'%';
        }
        $hegelv = DataModel::xueyuanArrList();
        $canyulv = DataModel::projectArrList();
        $high_weight = DataModel::xueyuanBmi();
        //print_r($high_weight);die();
        $man_level = DataModel::levelAvg();
        $woman_level = DataModel::levelAvg(2);
        $data = [
            //学校总人数
            "man_num"=> \app\admin\model\Memb::allNum(1),
            'women_num' => \app\admin\model\Memb::allNum(2),
            //参与体测人数
            'canyu_num' =>   $canyu_num,
            'canyu_rate' => $canyu_rate.'%',
            //体测合格率
            'hege_num' => $hege,
            'hege_rate' => $hege_rate,
            //体测平均分
            'avg_num' => DataModel::avg(),
            'max_avg' => DataModel::max(),
            'mix_avg' => DataModel::min(),
            //学院
            'xueyuan' => DataModel::xueyuan(),
            //各院系成绩合格率
            'hegelv' => [
                'data'=> $hegelv[0],
                'series' => $hegelv[1]
            ],
            //项目参与率
            'canyulv' => [
                'data' => $canyulv[0],
                'yzhou' => $canyulv[1],
            ],
            //运动时长
            'yundongshichang' => DataModel::ydshichang(),
            // 各院系平均分
            'yuanxi_avgs' => DataModel::yuanxiAvg(),
            //身高体重BMI
            'high_weight'=>
                [
                    'xueyuan' => $high_weight[0],
                    'weight' => $high_weight[1],
                    'high' => $high_weight[2],
                ],
            //bmi计算，算出这批学生的平均身高和体重，根据平均身高和体重，计算出BMI，然后再计算出BMI的显示比例
            'weight' => \app\admin\model\Data::selectBmi(),
            //男生
            'weight_man' =>\app\admin\model\Data::selectBmi(1),
            //女生
            'weight_woman' => \app\admin\model\Data::selectBmi(2),
            //各院系项目等级
            'project_level' => DataModel::projectLevel(),
            //男生各项目平均分与等级
            'man_level' =>
                [
                    'project' => $man_level[0],
                    'data' =>  $man_level[1]
                ],
            //女生各项目平均分与等级
            'woman_level' =>
                [
                    'project' => $woman_level[0],
                    'data' =>  $woman_level[1]
                ],
        ];
        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $data,
        );
        return $this->jsonReturn($message);
    }

    //院系画像
    public function yuanxi()
    {
        $departId = (int)input('departmentsId');
        if(empty($departId)){
            $departId = '25';// 默认值计算机专业
        }
        $row = \app\admin\model\Departments::where('id',$departId)->find();
        $pmap = [
            'status' => 1,
            'mark' => 1,
            'superior' => $departId,
        ];
        $pcount = \app\admin\model\Clss::where($pmap)->count();

        $pmap2 = [
            'status' => 1,
            'mark' => 1,

        ];
        if($departId == 33){
            $pmap2 += [
                'tiyu_superior' => $departId,
            ];
        }else{
            $pmap2 += [
                'superior' => $departId,
            ];
        }
        $total = \app\admin\model\Memb::where($pmap2)->count();
        //echo $total;die();
        $umap = [
            'user_type' => 1,
            'mark' => 1,
            'departmentsId' => $departId,
        ];
        $ucount = \app\admin\model\User::where($umap)->count();
        $dmap = [
            'mark'=>1,
            'status' => 1,
            ];
        $dcount = \app\admin\model\Equipment::where($dmap)->count();

        $canyulv = DataProf::projectArrList($departId);

        $hegelv = DataProf::xueyuanArrList($departId);

        $high_weight = DataProf::xueyuanBmi($departId);
        //print_r($high_weight);die();
        $man_level = DataProf::levelAvg($departId);
        $woman_level = DataProf::levelAvg($departId,2);
        $data = [
            'title' => $row?$row['name']:'',
            "total"=> $total, //院系总人数
            'zhuanye_num' => $pcount,
            'teacher_num'=> $ucount,//老师数量
            'device_num' => $dcount,//设备数量
            //
            //学院
            'xueyuan' => DataModel::departxueyuan($departId),
            //各项目参与率
            'canyulv' => [
                'data' => $canyulv[0],
                'yzhou' => $canyulv[1],
            ],
            //各院系成绩合格率
            'hegelv' => [
                'data'=> $hegelv[0],
                'series' => $hegelv[1]
            ],
            //运动时长
            'yundongshichang' => DataProf::ydshichang($departId),
            // 各院系平均分
            'yuanxi_avgs' => DataProf::yuanxiAvg($departId),
            //身高体重BMI
            'high_weight'=>
                [
                    'xueyuan' => $high_weight[0],
                    'weight' => $high_weight[1],
                    'high' => $high_weight[2],
                ],
            'weight' => DataProf::selectBmi($departId),
            //男生
            'weight_man' => DataProf::selectBmi($departId,1),
            //女生
            'weight_woman' => DataProf::selectBmi($departId,2),
            //各院系项目等级
            'project_level' => DataProf::projectLevel($departId),
            //男生各项目平均分与等级
            'man_level' =>
                [
                    'project' => $man_level[0],
                    'data' =>  $man_level[1]
                ],
            //女生各项目平均分与等级
            'woman_level' =>
                [
                    'project' => $woman_level[0],
                    'data' =>  $woman_level[1]
                ],
        ];
        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $data,
        );
        return $this->jsonReturn($message);
    }

    //专业
    public function zhuanye()
    {
        //professionalId
        $departId = (int)input('professionalId');
        if(empty($departId)){
            $departId = '197';// 默认值计算机专业
        }
        $row = \app\admin\model\Prof::where('id',$departId)->find();
        $pmap = [
            'status' => 1,
            'mark' => 1,
            'professional' => $departId,
        ];
        $pcount = \app\admin\model\Clss::where($pmap)->count();
        $pmap2 = [
            'status' => 1,
            'mark' => 1,
            //'professional' => $departId,
        ];
        if(input('departmentsId') == 33){
            $pmap2+=['tiyu_professional' => $departId];
        }else{
            $pmap2+=['professional' => $departId];
        }
        $total = \app\admin\model\Memb::where($pmap2)->count();
        $ucount = 0;
        $dmap = [
            'mark'=>1,
            'status' => 1,
        ];
        $dcount = \app\admin\model\Equipment::where($dmap)->count();
        $canyulv = DataClasses::projectArrList($departId);
        $hegelv = DataClasses::xueyuanArrList($departId);

        $high_weight = DataClasses::xueyuanBmi($departId);
        //print_r($high_weight);die();
        $man_level = DataClasses::levelAvg($departId);
        $woman_level = DataClasses::levelAvg($departId,2);
        $data = [
            'title' => $row?$row['name']:'',
            "total"=> $total, //院系总人数
            'zhuanye_num' => $pcount,
            'teacher_num'=> $ucount,//老师数量
            'device_num' => $dcount,//设备数量
            //
            //学院
            'xueyuan' => DataModel::profXueyuan($departId),
            //各项目参与率
            'canyulv' => [
                'data' => $canyulv[0],
                'yzhou' => $canyulv[1],
            ],
            //各院系成绩合格率
            'hegelv' => [
                'data'=> $hegelv[0],
                'series' => $hegelv[1]
            ],
            //运动时长
            'yundongshichang' => DataClasses::ydshichang($departId),
            // 各院系平均分
            'yuanxi_avgs' => DataClasses::yuanxiAvg($departId),
            //身高体重BMI
            'high_weight'=>
                [
                    'xueyuan' => $high_weight[0],
                    'weight' => $high_weight[1],
                    'high' => $high_weight[2],
                ],
            'weight' => DataClasses::selectBmi($departId),
            //男生
            'weight_man' => DataClasses::selectBmi($departId,1),
            //女生
            'weight_woman' => DataClasses::selectBmi($departId,2),
            //各院系项目等级
            'project_level' => DataClasses::projectLevel($departId),
            //男生各项目平均分与等级
            'man_level' =>
                [
                    'project' => $man_level[0],
                    'data' =>  $man_level[1]
                ],
            //女生各项目平均分与等级
            'woman_level' =>
                [
                    'project' => $woman_level[0],
                    'data' =>  $woman_level[1]
                ],
        ];
        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $data,
        );
        return $this->jsonReturn($message);
    }




    //班级概览
    public function banji()
    {
        $input = input();
        if(empty($input['classId'])){
            $id = 419;
        }else{
            $id = (int)$input['classId'];
        }
        $map = [];
        if ($id) {
           $map[] = ['classId', '=', $id];
        }
        $class = \app\admin\model\Clss::get($id);
        // if(empty($class)){
        //     return $this->jsonReturn(message('班级错误',false));
        // }
        $canyulv = DataModel::projectArrList($map);

        //DataClass::canyu();die();
        $man_level = DataModel::levelbanjiAvg($id,1);
        $woman_level = DataModel::levelbanjiAvg($id,2);
        $count = DataModel::yundongRate($id);

        $classRankList = DataModel::classRankTime($map);
        $gradeRankList = DataModel::classRankDis($map);
        $data = [
            'class_name' => $id?$class['name']:'全校',
            //运动总人数
            'yundong_num' => $count[0],
            'zong_num' => $count[1],//总人数
            'yundong_rate' => $count[2],//人数占比
            'man_rate' => $count[3],
            'woman_rate' => $count[4],
            'zong_time'=> $count[5],//运动总时长
            //各项目参与率
            'canyulv' => [
                'data' => $canyulv[0],
                'yzhou' => $canyulv[1],
            ],
            //运动时长
            'yundongshichang' => DataModel::banjiydshichang($id),
            //参与情况
            'canyun_ting'=> DataClass::canyu($map),
            //班级运动时长排名
            //运动总时长
            'class_rank1' => $classRankList,
            //跑步总里程
            'class_rank2' => $gradeRankList,
            //肺活量
            'feihuo' => DataClass::feihuo($id),
            //肺活量 男
            'feihuo_man' => DataClass::manFeihuo($id),
            //肺活量 女
            'feihuo_woman' => DataClass::womainFeihuo($id),
            //身高体重BMI
            'weight_bmi' => DataClass::selectBmi($id),
            //身高体重BMI
            'weight_bmi_man' => DataClass::selectBmi($id,1),
            //身高体重BMI
            'weight_bmi_woman' =>  DataClass::selectBmi($id,2),
            //男生各项目平均分与等级
            'man_level' =>
                [
                    'project' => $man_level[0],
                    'data' =>  $man_level[1]
                ],
            //女生各项目平均分与等级
            'woman_level' =>
                [
                    'project' => $woman_level[0],
                    'data' =>  $woman_level[1]
                ],

        ];

        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $data,
        );
        return $this->jsonReturn($message);
    }

    public function students()
    {
        //$this->userId;


        $input = input();
        $row = Db::table('think_user')->where('id',$this->userId)->find();
        if($row['user_type'] == 2 && empty($input['memberId'])){
            $student = \app\admin\model\Member::where('roll',$row['username'])->find();
            $id = $student?$student['id']:0;
        }else{

            //如果未传id，判断是否是学生，是学生传当前登录用户的学生ID
            if(empty($input['memberId'])){
                $id = 49;
            }else{
                $id = $input['memberId'];
            }
        }
        //echo $id;die();
        $memb = \app\admin\model\Memb::get($id);
        if(empty($memb)){
            return $this->jsonReturn(message('学生ID错误',false));
        }

     /*   DataClasses::tiaosheng($memb);
        die();*/
       /* DataModel::projectLevel2($memb);
        die();*/
        $classRankList = DataModel::classRankTime([['classId', '=', $memb['class']]]);
        $gradeRankList = DataModel::classRankDis([['classId', '=', $memb['class']]]);

        $organization = \app\admin\model\School::get($memb['organization']);
        $superior = \app\admin\model\Departments::get($memb['superior']);
        $calss = \app\admin\model\Clss::get($memb['class']);
        $professional = \app\admin\model\Professional::get($memb['professional']);

        $rmap = [];
        $rmap[] = ['memberId','=',$id];
        $rmap = array_merge_recursive($rmap,whereSchoolMap(false));
        $avg = SportRecord::where($rmap)->whereNotNull('pingscore')->avg('pingscore');


        $goodNum = DataModel::countRaningNum($rmap,1);
        $liangNum = DataModel::countRaningNum($rmap,2);
        $jigeNum = DataModel::countRaningNum($rmap,3);
        $noJigeNum = DataModel::countRaningNum($rmap,0);

        $total = $goodNum+$liangNum+$jigeNum+$noJigeNum;
        $hegeRate = cacl_rate($goodNum+$liangNum+$jigeNum,$total);

        //班级排名
        $where1[] = ['memberId','=',$memb['id']];
        $where2[] = ['classId','=',$memb['class']];

        $classRank = DataModel::sutrank($where1,$where2);

        $where3[] = ['gradeId', '=', $memb['grade']];
        $gradeRank =  DataModel::sutrank($where1,$where3);

        $stuBmi = DataModel::stuBmi([['memberId','=',$memb['id']]]);

        $canyun_ting = \app\admin\model\Data::stuCanyu([['memberId','=',$id]]);

        $data = [
            'name' => $memb['realname'],
            'avatar' => $memb['avatar'],
            'gende' => genderString($memb['gender']),
            'organization' => $organization?$organization['name']:'',
            'superior' => $superior?$superior['name']:'',
            'class' => $calss?$calss['name']:'',
            'professional' => $professional?$professional['name']:'',
            'roll' => $memb['roll'],
            'account' => $memb['username'],
            //成绩概览
            'hegelv' => $hegeRate,
            'score' => (int)$avg.'分',
            'class_rank'=> $classRank,
            'grade_rank' => $gradeRank,
            //身高体重BMI
            'high_bmi' =>[
                'data' => $stuBmi[0],
                'datas'=>  $stuBmi[1],
            ],
            //参与情况
            'canyun_ting'=> [
                'zong_num' => $canyun_ting[0],
                'zong_time' => $canyun_ting[1],
                'zong_avg' => $canyun_ting[2],
                'canyu_data' => $canyun_ting[3],
            ],
            //班级运动时长排名
            //运动总时长
            'class_rank1' =>$classRankList,
            //跑步总里程
            'class_rank2' =>$gradeRankList/*[
                ['id' => 12,'rank'=> 1,'no' => 20230340,'name' => '张小虎1','num' => '32.324',],
                ['id' => 12,'rank'=> 2,'no' => 20230340,'name' => '张小虎2','num' => '32.424',],
                ['id' => 12,'rank'=> 3,'no' => 20230340,'name' => '张小虎2','num' => '32.424',],
                ['id' => 12,'rank'=> 4,'no' => 20230340,'name' => '张小虎2','num' => '32.424',],
                ['id' => 12,'rank'=> 5,'no' => 20230340,'name' => '张小虎2','num' => '32.424',],
            ]*/,
            'class_curr1' => '班级'.$classRank,
            'class_curr2' => '年级'.$gradeRank,
            // 各院系项目平均分
            'project_avgs' => DataModel::projectLevel2($memb)
            /*[
                ['product', '2012', '2013', '2014', '2015', '2016', '2017'],
                ['Milk Tea', 56.5, 82.1, 88.7, 70.1, 53.4, 85.1],
                ['Matcha Latte', 51.1, 51.4, 55.1, 53.3, 73.8, 68.7],
                ['Cheese Cocoa', 40.1, 62.2, 69.5, 36.4, 45.2, 32.5],
                ['Walnut Brownie', 25.2, 37.1, 41.2, 18, 33.9, 49.1]
            ]*/,
            //各项目成绩
            'duanpao'=> DataClasses::duanpao($memb),
            'tiaosheng'=> DataClasses::tiaosheng($memb),
            'feihuo'=> DataClasses::stufeihuo($memb),
        ];

        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $data,
        );
        return $this->jsonReturn($message);
    }

    //跑步下拉项
    public function paobu()
    {
        $arr = [12,14,15,16,17,18,24];
        $list = \app\admin\model\Project::where('id','in',$arr)->field('id,name')->select();
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
        );
        return $this->jsonReturn($message);
    }

    //跳绳下拉项
    public function tiaosheng()
    {
        $arr = [
            //11,
            13,19,20,21,22,23];
        $list = \app\admin\model\Project::where('id','in',$arr)
            ->where('mark',1)
            ->where('type',0)
            ->field('id,name')
            ->select();
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
        );
        return $this->jsonReturn($message);
    }

    public function duanlian()
    {
        //$list = DataModel::ydsc();
        //$yd_zhanbi = DataModel::yundongZhanbi();die();

        $qx_yundong_num = DataModel::ticeren();
        $qx_num = \app\admin\model\Memb::allNum();
        $canyu_num = DataModel::ticeren();
        $map1[] = ['gender','=',1];
        $map1 = array_merge_recursive($map1,whereTimeMap(2));
        $man_rate =  DataModel::ticeren($map1);

        $map2[] = ['gender','=',2];
        $map2 = array_merge_recursive($map2, whereTimeMap(2));
        $woman_rate = DataModel::ticeren($map2);

        $total = $man_rate+$woman_rate;

        $map3 = whereTimeMap(2);
        $zong_time1 = SportRecord::where($map3)->sum('duration');
        $zong_time2 = SportRecord::where($map3)->where('unit','秒')->sum('score');
        $zong_time = $zong_time1+$zong_time2;
        $zong_time = cacl_rate($zong_time,3600);
        $yd_zhanbi = DataModel::yundongZhanbi(2);
        $data = [
            'qx_yundong_num'=> $qx_yundong_num,//全校运动人数
            'qx_num'=> $qx_num,//全校总人数
            'canyu_num' => $canyu_num,//参与人数
            'no_cany' => $qx_num - $canyu_num,//未参与人数
            'man_rate' => cacl_rate($man_rate,$total),//男女运动比例-男生比例
            'woman_rate' =>  cacl_rate($woman_rate,$total),//男女运动比例-女生比例
            'zong_time' => $zong_time,//运动总时长
            'man_num'=> \app\admin\model\Memb::allNum(1),//男生人数
            'woman_num' => \app\admin\model\Memb::allNum(2),//女生人数
            //各院系运动人数
            'yundong_list' => DataModel::yuanxihYundongNum(2),
            //各院系人均运动时长
            'yxrj_avg' => DataModel::ydsc(2),
            //各运动占比
            'yd_zhanbi' => [
                'xAxis_data'=>$yd_zhanbi[0],
                'series_data'=>$yd_zhanbi[1],
            ],
            //各运动成绩
            'chengji_zhanbi' => DataModel::ydcjZhanbi(2),
        ];
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $data,
        );
        return $this->jsonReturn($message);
    }


    public function saishi()
    {
        $departId = (int)input('departmentsId');
        if(empty($departId)){
            //$departId = '25';// 默认值计算机专业
            //返回结果
            $message = array(
                "msg" => '赛事ID必传',
                "code" => 1,
                "data" => '',
            );
            return $this->jsonReturn($message);
        }

        $pmap = [
            'status' => 1,
            'mark' => 1,
        ];
        $total = \app\admin\model\Memb::where($pmap)->count();
        //echo $total;die();
        $umap = [
            'user_type' => 1,
            'mark' => 1,
            'departmentsId' => $departId,
        ];
        $ucount = \app\admin\model\User::where($umap)->count();
        $dmap = [
            'mark'=>1,
            'status' => 1,
        ];
        $dcount = \app\admin\model\Equipment::where($dmap)->count();

        $map1 = [];
        $map1[] = ['taskId','=',$departId];
        $map1[] = ['type','>=',3];
        $canyu_num = DataModel::ticeren($map1);
        $canyu_rate = cacl_rate($canyu_num,$total);
        $hege = DataModel::ticerenHege($map1);
        $hege_rate = 0;
        if($hege > 0){
            $hege_rate = cacl_rate($hege,$canyu_num).'%';
        }

        $data = [
            'title' => '',
            "total"=> $total, //院系总人数
            //学校总人数
            "man_num"=> \app\admin\model\Memb::allNum(1),
            'women_num' => \app\admin\model\Memb::allNum(2),
            //参与体测人数
            'canyu_num' =>   $canyu_num,
            'canyu_rate' => $canyu_rate.'%',
            //体测合格率
            'hege_num' => $hege,
            'hege_rate' => $hege_rate,
            //体测平均分
            'avg_num' => DataModel::avg($map1),
            'max_avg' => DataModel::max($map1),
            'mix_avg' => DataModel::min($map1),
            'teacher_num'=> $ucount,//老师数量
            'device_num' => $dcount,//设备数量

            // 各院系平均分
            'yuanxi_avgs' => DataModel::yuanxiAvg($map1),

            //各院系项目等级
            'project_level' => DataModel::projectLevel($map1),

        ];
        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $data,
        );
        return $this->jsonReturn($message);
    }

}
